<cfinvoke component="components.data" method="category" returnvariable="categorys">
<cfinvoke component="components.data" method="five_star_area_category" returnvariable="area">
<cfinvoke component="components.data" method="expertise" returnvariable="expertise">
<cfinvoke component="components.data" method="current_selections" returnvariable="selections">
<cfparam default="0" name="url.action">
<cfcontent reset="yes">
<cfswitch expression="#url.action#">

<cfcase value="get_users">
<cfinvoke component="components.data" method="get_users" returnvariable="get_users">

<cfset result_message = ''>
	<cfset result_message = '{"users":['>
	
	<cfloop query="get_users">
		<cfset result_message =  result_message & '{"username":"' & Session.helpers.add_json_escapement(get_users.username) &   '"},'>
	</cfloop>
	<cfset result_message = Session.helpers.remove_comma_from_end_of_string(result_message)>
	<cfset result_message = result_message & ']}'>
<cfcontent type="text/html" reset="yes"><cfoutput>#result_message#</cfoutput>
</cfcase>

<cfcase value="delete_user">

<cfset num_errors = 0>
<cfset error_message = ''>
<cfset error_message = '{"error":['>

<cfif session.username eq "#username#">
	<cfset num_errors = num_errors + 1>
	<cfset error_message =  error_message & '{"field":"'&username&'","error_message":"<br>You can not delete yourself!."},'>
</cfif>

<cfif session.prac_admin eq "false">
	<cfset num_errors = num_errors + 1>
	<cfset error_message =  error_message & '{"field":"'&username&'","error_message":"<br>You do not have permission to delete!."},'>
</cfif>

<cfset error_message = Session.helpers.remove_comma_from_end_of_string(error_message)>

<cfset error_message =  error_message & ']}'>

<cfif num_errors EQ 0>

<cfinvoke component="components.data" method="delete_user"  id="#username#">
none
<cfelse>

<cfcontent type="text/html" reset="yes"><cfoutput>#error_message#</cfoutput>
</cfif>	
	
</cfcase>


<cfcase value="add_user">
<cfset num_errors = 0>
<cfset error_message = ''>
<cfset error_message = '{"error":['>

<cfif trim(username)  EQ "">
	<cfset num_errors = num_errors + 1>
	<cfset error_message =  error_message & '{"field":"add_user_name","error_message":"User name can not be blank."},'>
</cfif>

<cfif len(pass1) lte 4>
	<cfset num_errors = num_errors + 1>
	<cfset error_message =  error_message & '{"field":"add_password","error_message":"Password must have more than 4 characters."},'>
</cfif>

<cfif len(pass2) lte 4>
	<cfset num_errors = num_errors + 1>
	<cfset error_message =  error_message & '{"field":"add_password2","error_message":"Password must have more than 4 characters."},'>
</cfif>

<cfif trim(pass1) neq trim(pass2)>
	<cfset num_errors = num_errors + 1>
	<cfset error_message =  error_message & '{"field":"add_password","error_message":"Passwords must match."},'>
	<cfset error_message =  error_message & '{"field":"add_password2","error_message":"Passwords must match."},'>
</cfif>

<cfif NOT isValid("email", email) AND trim(email) NEQ "">
	<cfset num_errors = num_errors + 1>
	<cfset error_message =  error_message & '{"field":"add_email","error_message":"Not proper email format. ie. xxx@xxx.xx"},'>
</cfif>

<cfquery datasource="#session.dsn#" name="getRoles">
select username as testname from prac_roles
</cfquery>
<cfloop from="1" to="#getRoles.recordcount#" index="i">
<cfif getRoles.testname[i] eq "#username#">
	<cfset num_errors = num_errors + 1>
	<cfset error_message =  error_message & '{"field":"add_user_name","error_message":"This User name is already in the database."},'>
<cfelse>
</cfif>
</cfloop>


<cfset error_message = Session.helpers.remove_comma_from_end_of_string(error_message)>

<cfset error_message =  error_message & ']}'>

<cfif num_errors EQ 0>

<CFQUERY datasource="#session.dsn#"  >
insert into prac_roles (username)
values ('#username#');
</CFQUERY>
<CFQUERY datasource="#session.dsn#"  >
update prac_roles 
set
passcode = '#pass1#',
email = '#email#'
where username = '#username#'
</CFQUERY>
none
<cfelse>
<cfcontent type="text/html" reset="yes"><cfoutput>#error_message#</cfoutput>
</cfif>
</cfcase>


<cfcase value="edit_user">
<cfinvoke component="components.data" method="get_users" returnvariable="get_users">
<cfinvoke component="components.data" method="get_selected" returnvariable="get_selected" id="#username#">
<cfset selected_password = "#get_selected.passcode#">
<cfset selected_username = "#get_selected.username#">
<cfset selected_email = "#get_selected.email#">

<cfset result_message = ''>
	<cfset result_message = '{"users":['>
	
	<cfloop query="get_selected">
		<cfset result_message =  result_message & '{"username":"' & Session.helpers.add_json_escapement(get_selected.username) & '","selected_username":"' & Session.helpers.add_json_escapement(selected_username) & '","selected_email":"' & Session.helpers.add_json_escapement(selected_email) & '","selected_password":"' & Session.helpers.add_json_escapement(selected_password) & '"},'>
	</cfloop>
	<cfset result_message = Session.helpers.remove_comma_from_end_of_string(result_message)>
	<cfset result_message = result_message & ']}'>
<cfcontent type="text/html" reset="yes"><cfoutput>#result_message#</cfoutput>
</cfcase>

<cfcase value="save_edit">
<cfset num_errors = 0>
<cfset error_message = ''>
<cfset error_message = '{"error":['>

<cfif trim(username)  EQ "">
	<cfset num_errors = num_errors + 1>
	<cfset error_message =  error_message & '{"field":"add_user_name","error_message":"User name can not be blank."},'>
</cfif>

<cfif len(pass2) lte 4>
	<cfset num_errors = num_errors + 1>
	<cfset error_message =  error_message & '{"field":"add_password2","error_message":"Password must have more than 4 characters."},'>
</cfif>

<cfif len(passnew) lte 4>
	<cfset num_errors = num_errors + 1>
	<cfset error_message =  error_message & '{"field":"add_passwordnew","error_message":"Password must have more than 4 characters."},'>
</cfif>

<cfif trim(pass2) neq trim(passnew)>
	<cfset num_errors = num_errors + 1>
	<cfset error_message =  error_message & '{"field":"add_password2","error_message":"Passwords must match."},'>
	<cfset error_message =  error_message & '{"field":"add_passwordnew","error_message":"Passwords must match."},'>
</cfif>

<cfif NOT isValid("email", email) AND trim(email) NEQ "">
	<cfset num_errors = num_errors + 1>
	<cfset error_message =  error_message & '{"field":"add_email","error_message":"Not proper email format. ie. xxx@xxx.xx"},'>
</cfif>

<cfset error_message = Session.helpers.remove_comma_from_end_of_string(error_message)>

<cfset error_message =  error_message & ']}'>

<cfif num_errors EQ 0>

<CFQUERY datasource="#session.dsn#"  >
update prac_roles 
set
username = '#username#',
passcode = '#passnew#',
email = '#email#'
where username = '#old_username#'
</CFQUERY>

none
<cfelse>
<cfcontent type="text/html" reset="yes"><cfoutput>#error_message#</cfoutput>
</cfif>
</cfcase>

</cfswitch>